#include "scriptapplication.h"
#include "workbench.h"
#include "scriptmanager.h"
#include "projectsettings.h"

#include <QtGui/QDialog>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QDesktopWidget>
#if defined(QT_NO_SCRIPTTOOLS)
    #include <QMessageBox>
#endif

ScriptApplication::ScriptApplication(int argc, char *argv[]) :
    QApplication(argc, argv)
{
    QCoreApplication::setOrganizationName("Savushkin");
    QCoreApplication::setApplicationName("uiscripter");
    QCoreApplication::setApplicationVersion("1.0.0");
#ifdef Q_OS_WINCE
    m_osName = "wince";
#elif defined(Q_OS_WIN32)
    m_osName = "win32";
#elif defined(Q_OS_SYMBIAN)
    m_osName = "symbian";
#elif defined(Q_OS_ANDROID)
    m_osName = "android";
#elif defined(Q_OS_LINUX)
    m_osName = "linux";
#endif
}

ScriptApplication::~ScriptApplication()
{
    delete m_workbench;
}

void ScriptApplication::loadProject(const QString &projectFile)
{
    m_workbench = Workbench::instance();
    m_workbench->loadProject(projectFile);
}

void ScriptApplication::debug()
{
#if !defined(QT_NO_SCRIPTTOOLS)
    ScriptManager::instance()->debugger.action(QScriptEngineDebugger::InterruptAction)->trigger();
    ScriptManager::instance()->debugWindow->show();
#else
    QMessageBox::information(0,"No debugger","Programm compiled without script tools");
#endif
}

void ScriptApplication::about()
{
    QString s;
    s += "<table cellpadding=\"4\" ><tr><td><img src=\":/images/";
    if(QApplication::desktop()->physicalDpiX()>160)
        s += "brand";
    else
        s += "milk-jug";

    s += ".png\"></td><td>";
    s += tr("Application: ") + "<i>" + applicationName() + "</i><BR>";
    s += tr("Company: ") + "<i>" + organizationName() + "</i><BR>";
    s += tr("QT: ") + "<i>" + QLatin1String(QT_VERSION_STR)+"</i><BR>";
    s += tr("Version: ") + "<i>" + applicationVersion() + "</i>";
    s += "</td></tr></table>";

    QDialog *dialog = new QDialog(0,Qt::Window|Qt::WindowCloseButtonHint);
    QHBoxLayout *hlayout = new QHBoxLayout();
    QVBoxLayout *vlayout = new QVBoxLayout(dialog);
    QLabel *label = new QLabel();
    QPushButton *button = new QPushButton("Ok");
#ifdef Q_OS_ANDROID
    dialog->setWindowFlags(Qt::ToolTip);
    dialog->setStyleSheet(" QDialog { background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0.903, y2:0, stop:0 rgba(216, 223, 232, 252), stop:1 rgba(255, 255, 255, 255)); border: 1px solid black; border-style: solid; border-radius: 4px; }");
#endif
    dialog->setAttribute(Qt::WA_DeleteOnClose, true);

    vlayout->addWidget(label);
    label->setText(s);
    hlayout->addStretch(50);
    hlayout->addWidget(button);
    vlayout->addLayout(hlayout,0);
    connect(button, SIGNAL(clicked()), dialog, SLOT(close()));
    dialog->setWindowIcon(QIcon(":/images/milk-jug"));
    dialog->setWindowTitle(tr("About"));
    dialog->exec();
}
